package com.macro.mall.config;

import com.macro.mall.common.filter.DecryptReplaceStreamFilter;
import com.macro.mall.common.filter.ExceptionFilter;
import jakarta.servlet.Filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @ClassName :  WebAllHandlerConfig
 * @Description :  过滤器、监听器、拦截器  前置处理配置类
 * @Author :  AD
 */

@Configuration
public class WebAllHandlerConfig {

    /**
     * 注册过滤器
     */
    @Bean
    public FilterRegistrationBean AllFilterRegistration() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setFilter(decryptReplaceStreamFilter());
        registration.addUrlPatterns("/*");
        registration.setName("APISecurityFilter");
        registration.setOrder(1);
        return registration;
    }

    @Bean(name = "decryptReplaceStreamFilter")
    public Filter decryptReplaceStreamFilter() {
        return new DecryptReplaceStreamFilter();
    }

    @Bean
    public FilterRegistrationBean exceptionFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(exceptionFilter());
        registration.setName("exceptionFilter");
        //此处尽量小，要比其他Filter靠前
        registration.setOrder(-1);
        return registration;
    }

    @Bean(name = "exceptionFilter")
    public Filter exceptionFilter() {
        return new ExceptionFilter();
    }
}